
Page({
  data: {
    bgImg: '/images/user/mybg@2x.jpg', // 顶部背景图片
    __user_info__: {}, // 用户信息
    likeCount: 0, // 预约
    collectCount: 0, // 收藏数量
  },
  onLoad: function (t) {
    // this.handleBgImg();
    if (wx.canIUse('hideHomeButton') && wx.hideHomeButton) { wx.hideHomeButton() } 

  },
  onShow() {
    this.init();
    this.exInit();
  },
  onHide() {
    wx.removeStorageSync('_pageUrl');
  },
  onUnload() {
    wx.removeStorageSync('_pageUrl');
  },
  init() {
    // const tpage = getCurrentPages();
    // const thisPageUrl = tpage[tpage.length - 1].route;
    // console.log(tpage, thisPageUrl)
    if (getApp().globalData.userInfo && getApp().globalData.userInfo.token) {
      this.setData({ __user_info__: getApp().globalData.userInfo });
      this.getCount({memberId: getApp().globalData.userInfo.memberId, type: 1});
      this.getCount({memberId: getApp().globalData.userInfo.memberId, type: 2});
    } 
    if (!getApp().globalData.userInfo || !getApp().globalData.userInfo.token) {
      // this.getCode();
    }
  },
  handleBgImg() { // 背景图片获取
    let that = this;
    wx.getImageInfo({
      // src: 'https://i.postimg.cc/52Ny2K2t/mybg-2x.png',
      src: '/images/user/mybg@2x.jpg',
      success (res) {
        that.setData({ bgImg: res.path });
      },
      fail(err){
        console.log(err)
      }
    })
  },
  toPage(e) { // 页面跳转
    // if (!getApp().globalData.userInfo || !getApp().globalData.userInfo.token) {
    //   this.needLogin({type: 'userPage'});
    //   return false;
    // }
    if (e.currentTarget.dataset.url) {
      wx.navigateTo({ url: e.currentTarget.dataset.url })
    } else {
      console.warn('Not Page Url')
    }
  },
  // ----------------------------------------------------
  getCount(params) { // 获取我的楼盘收藏数量/我的楼盘预约数量
    getApp().api.getLikeAndCollect(params).then(res => {
      this.setData({
        [params.type == 1?'collectCount':'likeCount']: res.data || 0
      })
    })
  },
  // ---------------ex------------------
  exInit() {
    let userinfo = wx.getStorageSync('__user_info__');
    if (userinfo) {
      this.setData({
        __user_info__: {...userinfo, token: 'ASedfewqewaewqae4grdse46ytewedw'}
      })
    }
    wx.hideLoading();
  },
  handleUserInfo(e) {
    if (e.detail.detail.errMsg === "getUserInfo:ok") {
      wx.showLoading({
        title: '加载中...', //提示的内容,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => {}
      });
      wx.setStorageSync('__user_info__', JSON.parse(e.detail.detail.rawData))
      setTimeout(() => {
        this.exInit()
      }, 500)
    }
  }
})
